var app = getApp();
Page({
    data: {
        open: false,
        kong: true
    },
    onLoad: function t(e) {
        var a = this;
        var o = getApp().imgurl;
        var n = wx.getStorageSync("mdid");
        console.log(n);
        var i = wx.getStorageSync("UserData").id;
        console.log('sfsdfdfdfsfsdfdfdfsfsdfdfdfsfsdfdfdfsfsdfdfdfsfsdfdfdf');
        console.log(i);
        app.util.request({
            url: "entry/wxapp/system",
            cachetime: "0",
            success: function t(e) {
                console.log(e);
                a.setData({
                    xtxx: e.data,
                    url: o
                });
                wx.setNavigationBarColor({
                    frontColor: "#ffffff",
                    backgroundColor: e.data.link_color
                });
                wx.setNavigationBarTitle({
                    title: e.data.link_name
                });
                if (e.data.is_yue == "1") {
                    a.setData({
                        kqyue: true
                    })
                } else {
                    a.setData({
                        kqyue: false
                    })
                }
            }
        });
        app.util.request({
            url: "entry/wxapp/UserInfo",
            cachetime: "0",
            data: {
                user_id: i
            },
            success: function t(e) {
                console.log("用户信息", e.data);
                a.setData({
                    userInfo: e.data,
                    discount: e.data.discount
                });
                if (e.data.grade == "0") {
                    wx.showModal({
                        title: "提示",
                        content: "开卡成为会员能享受优惠买单哦~"
                    });
                    setTimeout(function() {
                        wx.redirectTo({
                            url: "../my/login"
                        })
                    }, 1500)
                }
            }
        });
        app.util.request({
            url: "entry/wxapp/StoreInfo",
            cachetime: "0",
            data: {
                id: n
            },
            success: function t(e) {
                console.log("门店信息", e.data);
                a.setData({
                    mdinfo: e.data
                });
                wx.setNavigationBarTitle({
                    title: "欢迎光临" + e.data.name
                })
            }
        });
        app.util.request({
            url: "entry/wxapp/Czhd",
            cachetime: "0",
            success: function t(e) {
                console.log(e);
                a.setData({
                    czhd: e.data
                })
            }
        })
        app.util.request({
            url: "entry/wxapp/getQr",
            cachetime: "0",
            data: {
                id: n,
                user_id: i
            },
            success: function t(e) {
            	console.log('sdfsfdfddf');
            	console.log(e.data);
                a.setData({
                    qrcode: e.data
                });
            }
        });
    },
    jsmj: function t(e, a) {
        var o;
        for (var n = 0; n < a.length; n++) {
            if (Number(e) >= Number(a[n].full)) {
                o = n;
                break
            }
        }
        return o
    },
    bindInput: function t(e) {
        console.log(e.detail.value);
        this.setData({
            czje: e.detail.value
        });
        if (e.detail.value != "") {
            this.setData({
                kong: false
            })
        } else {
            this.setData({
                kong: true
            })
        }
    },
    tradeinfo: function t() {
        var e = this;
        this.setData({
            open: !e.data.open
        })
    },
    formSubmit: function t(e) {
        console.log("form发生了submit事件，携带数据为：", e.detail, e.detail.formId);
        var a = getApp().getOpenId,
            o = e.detail.value.czje,
            n = this.data.czhd;
        var i = wx.getStorageSync("UserData").id;
        var s = this.data.mdinfo.id;
        console.log(n);
        if (n.length == 0) {
            var r = 0
        } else if (Number(o) >= Number(this.data.czhd[n.length - 1].full)) {
            var l = this.jsmj(o, n);
            console.log(l);
            var r = Number(n[l].reduction)
        } else {
            var r = 0
        }
        console.log(a, o, i, r, s);
        app.util.request({
            url: "entry/wxapp/AddCzOrder",
            cachetime: "0",
            data: {
                user_id: i,
                money: o,
                money2: r,
                store_id: s,
                form_id: e.detail.formId
            },
            success: function t(e) {
                console.log(e);
                if (e.data != "下单失败") {
                    app.util.request({
                        url: "entry/wxapp/pay2",
                        cachetime: "0",
                        data: {
                            openid: a,
                            money: o,
                            order_id: e.data
                        },
                        success: function t(e) {
                            console.log(e);
                            wx.requestPayment({
                                timeStamp: e.data.timeStamp,
                                nonceStr: e.data.nonceStr,
                                package: e.data.package,
                                signType: e.data.signType,
                                paySign: e.data.paySign,
                                success: function t(e) {
                                    console.log(e);
                                    wx.showToast({
                                        title: "充值成功",
                                        duration: 1e3
                                    });
                                    setTimeout(function() {
                                        wx.navigateBack({})
                                    }, 1e3)
                                },
                                complete: function t(e) {
                                    if (e.errMsg == "requestPayment:fail cancel") {
                                        wx.showToast({
                                            title: "取消支付",
                                            icon: "loading",
                                            duration: 1e3
                                        })
                                    }
                                }
                            })
                        }
                    })
                }
            }
        })
    },
    onReady: function t() {},
    onShow: function t() {},
    onHide: function t() {},
    onUnload: function t() {},
    onPullDownRefresh: function t() {
        wx.stopPullDownRefresh()
    },
    onReachBottom: function t() {}
});